package com.tvunetworks.center.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.device.annotation.Logined;
import com.tvunetworks.center.device.model.DeviceGeoRecord;
import com.tvunetworks.center.device.model.param.DeviceGeoRecordParam;
import com.tvunetworks.center.device.service.DeviceGeoRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
/**
 * @author lebron
 * @ClassName: MyMybatisProperties
 * @Description: Mybatis参数
 */
@Slf4j
@RestController
@RequestMapping("/geoRecord")
public class DeviceGeoRecordController {
    @Autowired
    private DeviceGeoRecordService deviceGeoRecordService;

    /**
     * 查询geo记录
     * @param loginUser loginUser
     * @param queryData queryData
     * @return Result
     */
    @PostMapping("/listGeoRecord")
    public Result listGeoRecord(@Logined LoginUser loginUser, @RequestBody String queryData){
        List<DeviceGeoRecord> list = null;
        DeviceGeoRecordParam deviceGeoRecordParam = JSONObject.parseObject(queryData,DeviceGeoRecordParam.class);
        try {
            if(loginUser ==null){
                return ResultUtil.noLogin();
            }

            list = deviceGeoRecordService.list(deviceGeoRecordParam);
        } catch (Exception e) {
            list = new ArrayList<>();
            log.error("deviceGeoRecordService.list Exception:[{}]",e);
        }
        return ResultUtil.success(list);
    }

    /**
     * 保存geo记录
     * @param loginUser loginUser
     * @param paramData 参数
     * @return success
     */
    @PostMapping("/saveGeoRecord")
    public Result saveGeoRecord(@Logined LoginUser loginUser, @RequestBody String paramData){
        DeviceGeoRecord deviceGeoRecord = JSONObject.parseObject(paramData, DeviceGeoRecord.class);
        if(loginUser ==null){
            return ResultUtil.noLogin();
        }
        if(StringUtils.isEmpty(deviceGeoRecord.getPeerId())){
            return ResultUtil.paramError();
        }
        if(StringUtils.isEmpty(deviceGeoRecord.getLatitude())){
            return ResultUtil.paramError();
        }
        if(StringUtils.isEmpty(deviceGeoRecord.getLongitude())){
            return ResultUtil.paramError();
        }
        if(deviceGeoRecord.getTimestamp() == null){
            return ResultUtil.paramError();
        }
        deviceGeoRecord.setUserId(loginUser.getId());
        deviceGeoRecordService.save(deviceGeoRecord);
        return ResultUtil.success();
    }

    /**
     * 测试
     */
     @PostMapping("/test")
    public void test(){
        deviceGeoRecordService.test();
    }


}
